import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;


public class ConnectWindow extends JFrame implements ActionListener {
	
	private JLabel hostLabel, portLabel, nickLabel;
	private JTextField hostTextField, portTextField, nickTextField;
	private JButton connectButton;
	private JPanel connectPanel, buttonPanel;
	
	private MainWindow mainWindow = null;
	boolean connecting;
	
	private Socket socket = null;
	private PrintWriter out = null;
	private BufferedReader in = null;
	
	public ConnectWindow()
	{
		super("Badugi - Connecting Window");
		hostLabel = new JLabel("Host");
		portLabel = new JLabel("Port");
		nickLabel = new JLabel("Nick");
		connectPanel = new JPanel();
		buttonPanel = new JPanel();
		connectButton = new JButton("Connect");
		hostTextField = new JTextField();
		portTextField = new JTextField();
		nickTextField = new JTextField();
		
		setLayout(new GridLayout(2,1));
		connectPanel.setLayout(new GridLayout(0,2));
		connectPanel.add(hostLabel);
		connectPanel.add(hostTextField);
		connectPanel.add(portLabel);
		connectPanel.add(portTextField);
		connectPanel.add(nickLabel);
		connectPanel.add(nickTextField);
		
		buttonPanel.add(connectButton);
		
		add(connectPanel);
		add(buttonPanel);
		
		setBounds(130,130,400,200);
		setResizable(false);
		setVisible(true);
		connectButton.addActionListener(this);
		connecting = false;
		
		addWindowListener( new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
            	if (connecting == true)
            	{
				     out.println("Q");
				     try
					 {
						 in.close();
						 out.close();
						 socket.close();
					 }
				     catch(IOException ex)
				     {
				    	 System.out.println("error in closing socket and I/O");
				     }
            	}
	            	System.exit(0);
            }
			   });
	}
	public void actionPerformed(ActionEvent event) 
	{
		int port = 0;
		if(event.getSource() == connectButton) 
		{
			try
			{
				port = Integer.parseInt(portTextField.getText());
			}
			catch(NumberFormatException e)
			{
				 JOptionPane.showMessageDialog(null, "Port have to be a number");
				 return;
			}
			try 
			{
				System.out.println("tworze wejscie");
				socket = new Socket(hostTextField.getText(), port);
				out = new PrintWriter(socket.getOutputStream(), true);
				in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				System.out.println("stworzylem");
				out.println(nickTextField.getText());
			}
			catch (UnknownHostException e) 
			{
				JOptionPane.showMessageDialog(null, "Unknown host: " + hostTextField.getText());
				return;
			}
			catch  (IOException e) 
			{
				System.out.println("No I/O"); System.exit(1);
			}
			connecting = true;
			System.out.println("wylaczam ikony");
			connectButton.setEnabled(false);
			hostTextField.setEnabled(false);
			nickTextField.setEnabled(false);
			portTextField.setEnabled(false);
			connectButton.setText("Wait...");
			System.out.println("wylaczylem ikony");
			(new Thread() {
				public void run() {
				    // do stuff
					while (true)
					{
						String str = new String();
						try 
						{
							System.out.println("waiting for server info");
							str=in.readLine();
						}
						catch (IOException e) 
						{
							
							System.exit(1);
						}
						System.out.println(str);
						if (str.equals("ready")) break;
					}
					setVisible(false);
					mainWindow = new MainWindow(socket, out, in, nickTextField.getText());
					//mainWindow.run();
				}
			}).start();
			
		}
	}
}
